/* QAD_WorkOrderReleasePrint.p - Release / Print Work Orders                  */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy woworl.p for use by QAD Reporting Framework.         */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}
{{&US_PX}pxmaint.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */
define temp-table ttMain no-undo
   define unique_id  as   integer
.

define temp-table ttPickList no-undo
   field wod_part    like wod_det.wod_part
   field com_rev     like pt_mstr.pt_rev
   field lad_loc     like lad_det.lad_loc
   field lad_lot     like lad_det.lad_lot
   field wod_qty_all like wod_det.wod_qty_all
   field um          like pt_mstr.pt_um
   field issued      as   character
   field unique_id   as   integer
   field ref_id      like ttMain.unique_id
.

define temp-table ttRouting no-undo
   field wr_op      like wr_route.wr_op
   field wr_ctr_mch like wr_route.wr_wkctr
   field wr_std_op  like wr_route.wr_std_op
   field wr_tool    like wr_route.wr_tool
   field wr_run     like wr_route.wr_run
   field temp1      as   character
   field temp2      as   character
   field unique_id  as   integer
   field ref_id     like ttMain.unique_id
.

define temp-table ttCoByProduct no-undo
   field wo_part    like wo_mstr.wo_part
   field wo_lot     like wo_mstr.wo_lot
   field lad_lot    like lad_det.lad_lot
   field wo_qty_chg like wo_mstr.wo_qty_chg
   field um         like pt_mstr.pt_um
   field received   like wo_mstr.wo_qty_comp
   field unique_id  as   integer
   field ref_id     like ttMain.unique_id
.

define temp-table tt_wo_mstr no-undo like wo_mstr
   field par_rev      like pt_mstr.pt_rev
   field wod_iss_date like wod_det.wod_iss_date
   field wo_des       like pt_mstr.pt_desc1
   field wo_um        like pt_mstr.pt_um
   field deliv        like wod_det.wod_deliver
   field cmt_cmmt     like cmt_det.cmt_cmmt
   field unique_id    as   integer
   field ref_id       like ttMain.unique_id
.

define temp-table ttReportOptions no-undo
   field wrnbr           like wo_mstr.wo_nbr
   field wrlot           like wr_route.wr_lot
   field print_pick      like mfc_ctrl.mfc_logical
   field print_rte       like mfc_ctrl.mfc_logical
   field print_jp        like mfc_ctrl.mfc_logical
   field deliv           like wod_det.wod_deliver
   field barcode         like mfc_ctrl.mfc_logical
   field move            like mfc_ctrl.mfc_logical
   field upd_rel_date    like mfc_ctrl.mfc_logical
   field incl_zero_reqd  like mfc_ctrl.mfc_logical
   field incl_zero_open  like mfc_ctrl.mfc_logical
   field incl_pick_qtys  like mfc_ctrl.mfc_logical
   field incl_floor_stk  like mfc_ctrl.mfc_logical
   field jp_1st_last_doc like mfc_ctrl.mfc_logical
.

define dataset dsReportResults for ttPickList, ttRouting, ttCoByProduct, tt_wo_mstr, ttReportOptions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS as handle    no-undo.

empty temp-table ttPickList      no-error.
empty temp-table ttRouting       no-error.
empty temp-table ttCoByProduct   no-error.
empty temp-table tt_wo_mstr      no-error.
empty temp-table ttReportOptions no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_WorkOrderReleasePrint.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "print_pick" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "print_rte" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "print_jp" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "barcode" , "DataFormat" , GetLabel("YES/NO")).

   define variable move like woc_ctrl.woc_move no-undo.
   for first woc_ctrl  where woc_ctrl.woc_domain = global_domain no-lock:
      if available woc_ctrl then
        move = woc_move.
   end. /* for first woc_ctrl where ... */
   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "move" , "DataFormat" , GetLabel("YES/NO")).
   run SetFieldMetaParameter in  ipReporthandle(
       "ttReportOptions", "move" , "DefaultValue1" , move).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "upd_rel_date" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "incl_zero_reqd" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "incl_zero_open" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "incl_pick_qtys" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "incl_floor_stk" , "DataFormat" , GetLabel("YES/NO")).

   run SetFieldMetaParameter in ipReportHandle(
       "ttReportOptions", "jp_1st_last_doc" , "DataFormat" , GetLabel("FIRST/LAST")).

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DISPALY COMMENT */
PROCEDURE DisplayComment:

   define input-output parameter comment like com_det.cmt_cmmt.
   define input        parameter type    like cmt_det.cmt_type.
   define input        parameter id      like cmt_det.cmt_id.

   /* l = local */
   define variable l_count as integer initial 1 no-undo.

   for each cmt_det no-lock
      where cmt_det.cmt_domain = global_domain
        and     (cmt_indx = id
            and cmt_indx > 0
            and    (lookup(type,cmt_print) > 0
                or type = "RP")
                ):

      do i = 1 to 15:

         if cmt_cmmt[i] <> ""
         then do:
         assign
            comment[row_count] = cmt_cmmt[i]
            row_count          = row_count + 1
         .
         end. /* if cmt_cmmt[i] <> "" */

      end. /* do i = 1 to 15 */

   end. /* for each cmt_det no-lock */

END PROCEDURE /* PROCEDURE DisplayComment */

/* PRINT WORK ORDER PICKLIST */
PROCEDURE PrintPickList:

   define input-output parameter picklistprinted like mfc_logical.
   define input parameter deliv like wod_deliver.
   define input parameter wo_des like pt_desc1.
   define input parameter wo_qty like wo_qty_ord.
   define input parameter wo_um like pt_um.
   define input parameter wo_recno as recid.

   define variable i as integer.
   define variable par_rev like pt_rev.

   define variable description like pt_desc1.
   define variable um like pt_um.
   define variable loc like pt_loc.
   define variable issued as character initial "(      )" label "Issued".
   define variable com_rev like pt_rev label "Rv".
   define variable issue-date like wod_iss_date.
   define variable qtyall like lad_qty_all.
   define variable c-cont as character format "x(18)" no-undo.
   define variable c-quantity as character format "x(18)" no-undo.
   define variable c-not-avail as character format "x(18)" no-undo.
   define variable c-msg-text  as character format "x(45)" no-undo.
   define variable l_pt_lot_ser as character no-undo.

   define new shared variable wod_recno as recid.
   define new shared variable fas_unit_qty as character.

   define workfile floorstk no-undo
      field fs_part as character label "Floor Stock" format "x(28)"
      field fs_qty like wod_qty_req.

   find wo_mstr no-lock where recid(wo_mstr) = wo_recno no-error.
   if not available wo_mstr then
      leave.

   assign
      c-cont = "*** " + getTermLabel("CONTINUED",18) + " ***"
      c-quantity = getTermLabel("QUANTITY",18)
      c-not-avail = getTermLabel("NOT_AVAILABLE",18) + ":".

   form
      skip (1)
      wo_nbr         colon 13
      wod_iss_date   colon 68
      wo_lot         colon 13
      wo_batch       colon 13
      wo_part        colon 13
      par_rev        colon 40 label "Rev"
      wo_due_date    colon 68 label "Work Order Due Date" skip
      wo_des         no-label format "x(49)" at 15
      wo_rmks        colon 13
      wo_so_job      colon 68
      wo_qty         colon 13
      wo_um          no-label
      deliv          colon 68 skip (1)
   with frame picklist page-top side-labels no-attr-space width 80
   title (getFrameTitle("WORK_ORDER_PICKLIST",25)).

   /* SET EXTERNAL LABELS */
   setFrameLabels(frame picklist:handle).

   /* Read control file for configured product unit control flag */
   do transaction:
      fas_unit_qty = string(false).      /*DEFAULT VALUE*/
      find first fac_ctrl where fac_ctrl.fac_domain = global_domain
      no-lock no-error.
      if available fac_ctrl then
         fas_unit_qty = string(fac_unit_qty).
   end.

   find pt_mstr where pt_mstr.pt_domain = global_domain and  pt_part = wo_part
   no-lock no-error.

   assign
      wo_des = ""
      wo_um = ""
      par_rev = "".

   if available pt_mstr
   then do:

      assign
         wo_des = pt_desc1
         wo_des = wo_des + " " + pt_desc2
         wo_um = pt_um.

      /*  REVISION NUMBER IS DISPLAYED FROM PTP_DET TO GET LATEST  */
      /*  MODIFIED REVISION NUMBER                                 */
      for first ptp_det where ptp_det.ptp_domain = global_domain
         and ptp_part = wo_part
         and ptp_site = wo_site
      no-lock: end.

      if available ptp_det then
         par_rev = ptp_rev .
      else
         par_rev =  pt_rev .

   end.

   issue-date = ?.

   for each wod_det where wod_det.wod_domain = global_domain
      and wod_lot = wo_lot
   no-lock:

      if issue-date = ? or issue-date > wod_iss_date then
         issue-date = wod_iss_date.

      if incl_floor_stk
      then do:

         find pt_mstr where pt_mstr.pt_domain = global_domain
            and pt_part = wod_part
         no-lock no-error.
         find ptp_det where ptp_det.ptp_domain = global_domain
            and ptp_part = wod_part
            and ptp_site = wod_site
         no-lock no-error.

         if (available ptp_det and ptp_iss_pol = no)
            or (not available ptp_det
            and available pt_mstr and pt_iss_pol = no)
         then do:
            find last floorstk where fs_part < wod_part no-error.
            create floorstk.
            assign
               fs_part = wod_part
               fs_qty = wod_qty_req.
         end.

      end.

   end.

   for each wod_det where wod_det.wod_domain = global_domain
      and wod_lot = wo_lot
      and ((wod_qty_req = 0 and incl_zero_reqd)
        or wod_qty_all <> 0
        or (wod_qty_pick <> 0 and incl_pick_qtys)
        or (max(wod_qty_req - wod_qty_iss,0) = 0 and incl_zero_open)
        or (max(wod_qty_req - wod_qty_iss,0) > 0 and wod_qty_all <> 0))
   exclusive-lock
   break by wod_lot by wod_iss_date by wod_part
   with frame detail no-box down:

      picklistprinted = yes.

      if first-of(wod_iss_date)
      then do:
         hide frame picklist.
         page.
         display wod_iss_date with frame picklist.
      end.

      if first-of(wod_lot)
      then do:
         display
            wo_nbr
            wo_lot
            wo_batch
            wo_part
            par_rev
            wod_iss_date
            wo_due_date
            wo_des
            wo_rmks
            wo_so_job
            wo_qty
            wo_um deliv
         with frame picklist.

         for each sod_det where sod_det.sod_domain = global_domain
            and sod_nbr = wo_so_job
            and sod_compl_stat = ""
         no-lock:

            if sod_fa_nbr = wo_nbr and sod_lot = wo_lot
            then do:
               {us/gp/gpcmtprt.i &type=RP &id=sod_cmtindx &pos=5}
            end.

            else
            if sod_fa_nbr = wo_nbr and sod_lot = "" and fas_unit_qty = string(true)
            then do:

               find pt_mstr where pt_mstr.pt_domain = global_domain
                  and pt_part = sod_part
               no-lock no-error.

               if available pt_mstr then do:
                  {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
                           &PARAM = "( input  sod_part,
                                       input  wod_site,
                                       input ""pt_lot_ser"",
                                       output l_pt_lot_ser)"
                           &NOAPPERROR = true
                           &CATCHERROR = true}
               end.
               if available pt_mstr and l_pt_lot_ser = "S"
               then do:
                  {us/gp/gpcmtprt.i &type=RP &id=sod_cmtindx &pos=5}
               end.

            end.

         end.

      end.
      else
         view frame picklist.

      /*DISPLAY COMMENTS */
      if first-of(wod_iss_date)
      then do:
         {us/gp/gpcmtprt.i &type=RP &id=wo_cmtindx &pos=3}
      end.

      assign
         um = ""
         loc = ""
         com_rev = ""
         loc = wod_loc
         description = "".

      find pt_mstr where pt_mstr.pt_domain = global_domain
         and pt_part = wod_part
      no-lock no-error.

      find ptp_det where ptp_det.ptp_domain = global_domain
         and ptp_part = wod_part
         and ptp_site = wod_site
      no-lock no-error.

      if (available ptp_det and ptp_iss_pol = no)
         or (not available ptp_det
         and available pt_mstr and pt_iss_pol = no)
      then
         next.

      if available pt_mstr
      then do:

         um = pt_um.

         if loc = "" then do:
            {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
                     &PARAM = "(input  wod_part,
                                input  wod_site,
                                input ""pt_loc"",
                                output loc)"
                     &NOAPPERROR = true
                     &CATCHERROR = true}
         end.

         assign
            description = pt_desc1
            description = description + " " + pt_desc2.

         if available ptp_det then
            com_rev = ptp_rev.
         else
            com_rev = pt_rev.
      end.

      display
         wod_part
         com_rev
         wod_site @ lad_loc     
         ""       @ lad_lot     
         max(wod_qty_req - wod_qty_iss,0)
                  @ wod_qty_all 
         um
         ""       @ issued
      with frame detail width 80 no-attr-space.
      down 1 with frame detail.

      if available pt_mstr and pt_desc1 <> ""
      then do:

         put space(3) pt_desc1 skip.

      end.

      if available pt_mstr and pt_desc2 <> ""
      then do:

         put space(3) pt_desc2 skip.

      end.

      /*DISPLAY ALLOCATION DETAIL*/
      for each lad_det where lad_det.lad_domain = global_domain
         and lad_dataset = "wod_det"
         and lad_nbr = wod_lot
         and lad_line = string(wod_op)
         and lad_part = wod_part
      with frame detail:

         find ld_det where ld_det.ld_domain = global_domain
            and ld_site = lad_site
            and ld_loc = lad_loc
            and ld_part = lad_part
            and ld_lot = lad_lot
            and ld_ref = lad_ref
         no-lock no-error.

         accumulate lad_qty_all (total).

         if incl_pick_qtys then
            qtyall = lad_qty_all + lad_qty_pick.
         else
            qtyall = lad_qty_all.

         display
            lad_loc
            lad_lot
            qtyall @ wod_qty_all
            issued

         display
            lad_ref @ lad_lot

         if available ld_det and ld_expire <> ? then
            display
               ld_expire @ lad_loc


         /*IF QTY OH - QTY ALL TO OTHER ORDERS < QTY ALL TO THIS ORDER*/
         if not available ld_det
            or ld_qty_oh - ld_qty_all + lad_qty_all < lad_qty_all
         then do:

               /*Quantity not available at this location*/
               {us/bbi/pxmsg.i &MSGNUM=4992 &ERRORLEVEL=1 &MSGBUFFER=c-msg-text}

               put c-msg-text at 20 skip.
         end.

         /*UPDATE QTY PICKED*/
         assign
            lad_qty_pick = lad_qty_pick + lad_qty_all
            lad_qty_all = 0.

      end.

      if wod_qty_all > accum total(lad_qty_all)
         then do with frame detail:


         display
            "      ********" @ wod_part
            c-quantity @ lad_loc
            c-not-avail @ lad_lot
            wod_qty_all - accum total (lad_qty_all) @ wod_qty_all
            "********" @ issued
         with frame detail.

      end.

      assign
         wod_qty_pick = wod_qty_pick + accum total(lad_qty_all)
         wod_qty_all = wod_qty_all - accum total(lad_qty_all).

   end. /*FOR EACH*/

   if incl_floor_stk
   then do:
      find first floorstk no-error.

      if available floorstk
      then do:

         if picklistprinted = no
         then do:

            hide frame picklist.
            page.

            display
               wo_nbr
               wo_lot
               wo_batch
               wo_part
               par_rev
               wo_due_date
               issue-date @ wod_iss_date
               wo_des
               wo_rmks
               wo_so_job
               wo_qty
               wo_um
               deliv
            with frame picklist.

            for each sod_det where sod_det.sod_domain = global_domain
               and sod_nbr = wo_so_job
               and sod_compl_stat = ""
            no-lock:
               if sod_fa_nbr = wo_nbr and sod_lot = wo_lot
               then do:
                  {us/gp/gpcmtprt.i &type=RP &id=sod_cmtindx &pos=5}
               end.
            end.

            /* DISPLAY COMMENTS */
            {us/gp/gpcmtprt.i &type=RP &id=wo_cmtindx &pos=3}

            picklistprinted = yes.

         end. /*if picklistprinted = no.*/

      end.

      if available floorstk then

      for each floorstk with frame floorstk width 80 no-attr-space down:

         find pt_mstr where pt_mstr.pt_domain = global_domain
            and pt_part = fs_part
         no-lock no-error.

         display
            space(3)
            fs_part
            fs_qty
            pt_um.

         if pt_desc1 <> "" then display "   " + pt_desc1 @ fs_part.
         if pt_desc2 <> "" then display "   " + pt_desc2 @ fs_part.

         delete floorstk.

      end.

   end.

END PROCUDURE. /* PROCEDURE PrintPickList */

/* PRINT WORK ORDER ROUTING */
PROCEDURE PrintRouting:

   define input-output parameter routingprinted like mfc_logical.
   define input parameter deliv like wod_deliver.
   define input parameter barcode like mfc_logical.
   define input parameter wo_des like pt_desc1.
   define input parameter wo_qty like wo_qty_ord.
   define input parameter wo_um like pt_um.
   define input parameter wo_recno as recid.
   define input parameter print_loc like mfc_logical.
   define input parameter prd_recno as recid.

   define variable wc_description like wc_desc.
   define variable i as integer.
   define variable par_rev like pt_rev.


   define variable wr_ctr_mch like wr_wkctr format "x(24)".
   define variable temp1 as character format "x(6)".
   define variable temp2 as character format "x(4)".
   define variable commentindex like cmt_indx.

   define variable l_yield like pt_yield_pct initial 100.

   find wo_mstr no-lock where recid(wo_mstr) = wo_recno no-error.
   if not available wo_mstr then leave.

   find prd_det where recid(prd_det) = prd_recno no-lock no-error.

   /* ********** End Translatable Strings Definitions ********* */

   find pt_mstr  where pt_mstr.pt_domain = global_domain and  pt_part = wo_part
   no-lock no-error.
   assign
      wo_des = ""
      wo_um = ""
      par_rev = "".
   if available pt_mstr then do:
      assign
         wo_des = pt_desc1
         wo_des = wo_des + " " + pt_desc2
         wo_um = pt_um.

      /*  REVISION NUMBER IS DISPLAYED FROM PTP_DET TO GET LATEST  */
      /*  MODIFIED REVISION NUMBER                                 */

      for first ptp_det
             where ptp_det.ptp_domain = global_domain and  ptp_part = wo_part
            and   ptp_site = wo_site
            no-lock:
      end. /* FOR FIRST PTP_DET */

      if available ptp_det then assign par_rev = ptp_rev .
      else assign par_rev = pt_rev .

   end.

   for each wr_route  where wr_route.wr_domain = global_domain and  wr_lot =
   wo_lot break by wr_lot by wr_op
      with frame d:
      routingprinted = yes.

      if first-of (wr_lot) and barcode then do:
         {us/mf/mfprtbar.i wr_lot}
         {us/mf/mfprtbar.i wr_lot}
      end.
      if first-of (wr_lot) then do:
         create tt_wo_mstr.
         buffer-copy wo_mstr to tt_wo_mstr no-error.
         assign
            tt_wo_mstr.par_rev = par_rev
            tt_wo_mstr.deliv = deliv
         .

         DisplayComment().

      end.

      wr_ctr_mch = wr_wkctr.

      if wr_mch <> "" then
         wr_ctr_mch = wr_ctr_mch + "  " + getTermLabel("MACHINE",6)
                    + ": " + wr_mch.

      wc_description = "".
      find wc_mstr  where wc_mstr.wc_domain = global_domain and  wc_wkctr =
      wr_wkctr
         and wc_mch   = wr_mch   no-lock no-error.
      if available wc_mstr then wc_description = wc_desc.

      create ttRouting.
      assign
         ttRouting.wr_op      = wr_op
         ttRouting.wr_ctr_mch = wr_ctr_mch
         ttRouting.wr_std_op  = wr_std_op
         ttRouting.wr_tool    = wr_tool
         ttRouting.wr_run     = wr_setup
         ttRouting.temp1      = "______"
         ttRouting.temp2      = "  "
      .

      if (not available ptp_det
          and pt_op_yield  = yes)
      or (available ptp_det
          and ptp_op_yield = yes)
      then do:
         display
            wc_description @ wr_ctr_mch
            wr_vend @ wr_tool
            wr_run * ((wr_qty_ord) * (l_yield * .01))
               when (wr_run * wr_qty_ord <= 1.0)
            @ wr_run
            round(wr_run * wr_qty_ord,3) * (l_yield * .01)
               when (wr_run * wr_qty_ord > 1.0)
            @ wr_run
            "______" @ temp1
            "(  )" @ temp2.

         create ttRouting.
         assign
            ttRouting.wr_ctr_mch = wc_description
            ttRouting.wr_tool    = wr_vend
            ttRouting.wr_run     =   
               if (wr_run * wr_qty_ord <= 1.0)
                  then wr_run * ((wr_qty_ord) * (l_yield * .01))
                  else round(wr_run * wr_qty_ord,3) * (l_yield * .01)
            ttRouting.temp1 = "______"
            ttRouting.temp2 = "  "
         .

         l_yield = l_yield * (wr_yield * .01).
      end. /* IF (NOT AVAILABLE ptp_det */

      else

         create ttRouting.
         assign
            ttRouting.wr_ctr_mch = wc_description
            ttRouting.wr_tool    = wr_vend
            ttRouting.wr_run = if (wr_run * wr_qty_ord <= 1.0)
               then wr_run * wr_qty_ord
               else  round(wr_run * wr_qty_ord,3)
            ttRouting.temp1 = "______"
            ttRouting.temp2 = "  "
         .

      if wr_desc <> "" then do:
         create ttRouting.

         assign
            ttRouting.wr_ctr_mch = wr_desc
         .

      end.

      if barcode then do:
         {us/mf/mfprtbar.i string(wr_op)}
         {us/mf/mfprtbar.i string(wr_op)}
      end.

      /* PRINT ROUTING COMMENTS */
      if wr_cmtindx <> 0 then
         find first cmt_det  where cmt_det.cmt_domain = global_domain and
         cmt_indx = wr_cmtindx no-lock no-error.
      else do:

         find ro_det  where ro_det.ro_domain = global_domain and (
            ro_routing = (if wo_routing <> "" then wo_routing else wo_part)
            and ro_op = wr_op
            and (ro_start = ? or ro_start <= today)
            and (ro_end = ? or ro_end >= today)
            ) no-lock no-error.

         if available ro_det and ro_cmtindx <> 0 then do:
            find first cmt_det  where cmt_det.cmt_domain = global_domain and
            cmt_indx = ro_cmtindx
               no-lock no-error.
         end.
         else do:
            find opm_mstr  where opm_mstr.opm_domain = global_domain and
            opm_std_op = wr_std_op no-lock no-error.
            if available opm_mstr and opm_cmtindx <> 0 then
               find first cmt_det  where cmt_det.cmt_domain = global_domain and
               cmt_indx = opm_cmtindx
               no-lock no-error.
         end.
      end.

      if available cmt_det then do:
      commentindex = cmt_indx.
      DisplayComment()
      {us/gp/gpcmtprt.i &type=RP &id=commentindex &pos=3
         &command="down 1 with frame d."}
   end.

END PROCUDURE. /* PROCEDURE PrintRouting */

/* PRINT WORK ORDER JOINT PRODUCT EXPECTED RECEIPTS */
PROCEDURE PrintCoByProduct:

   define input-output parameter jpprinted   like mfc_ctrl.mfc_logical.
   define input parameter deliv              like wod_det.wod_deliver.
   define input parameter wo_des             like pt_mstr.pt_desc1.
   define input parameter wo_qty             like wo_mstr.wo_qty_ord.
   define input parameter wo_um              like pt_mstr.pt_um.
   define input parameter wo_recno           as recid.

   define variable i                as integer no-undo.
   define variable par_rev          like pt_mstr.pt_rev no-undo.
   define variable description      like pt_mstr.pt_desc1 no-undo.
   define variable um               like pt_mstr.pt_um no-undo.
   define variable loc              like pt_mstr.pt_loc no-undo.
   define variable wonbr            like wo_mstr.wo_nbr no-undo.
   define variable received         like wo_mstr.wo_qty_comp no-undo.
   define variable unique_id_coby   as integer no-undo.

   for first wo_mstr no-lock where recid(wo_mstr) = wo_recno:
   end.

   for first pt_mstr where pt_mstr.pt_domain = global_domain
      and pt_part = wo_part no-lock:
   end.
   assign
      wo_des   = ""
      wo_um    = ""
      par_rev  = "".

   if available pt_mstr
   then do:
      assign
         wo_des   = pt_desc1
         wo_des   = wo_des + " " + pt_desc2
         wo_um    = pt_um
         par_rev  = pt_rev.
   end.

   wonbr = wo_nbr.

   create tt_wo_mstr.
   buffer-copy wo_mstr to tt_wo_mstr no-error.
   assign 
      tt_wo_mstr.par_rev = par_rev
      tt_wo_mstr.deliv = deliv
   .

   jpprinted = yes.

   for each wo_mstr exclusive-lock  where wo_mstr.wo_domain = global_domain
      and wo_nbr = wonbr and wo_joint_type <> "5" break by wo_part:

      for first pt_mstr where pt_mstr.pt_domain = global_domain
         and  pt_part = wo_part no-lock:
      end.
      if available pt_mstr
      then do:

         assign
            um          = pt_um
            description = pt_desc1.

         {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
                  &PARAM = "(input  wo_part,
                             input  wo_site,
                             input ""pt_loc"",
                             output loc)"
                  &NOAPPERROR = true
                  &CATCHERROR = true
         }
      else do:
         assign
            um          = ""
            loc         = ""
            description = "".
      end.

      unique_id_coby = unique_id_coby + 1.
      create ttCoByProduct.
      assign
         ttCoByProduct.wo_part      = wo_mstr.wo_part
         ttCoByProduct.wo_lot       = wo_mstr.wo_lot
         ttCoByProduct.lad_lot      = " "
         ttCoByProduct.wo_qty_chg   = max(wo_mstr.wo_qty_ord
                                       - wo_mstr.wo_qty_comp,0)
         ttCoByProduct.um           = um
         ttCoByProduct.received     = "_____________"
         ttCoByProduct.description  = description
         ttCoByProduct.unique_id    = unique_id_coby
      .

      if available pt_mstr and pt_desc2 <> ""
      then do:
         assign
            ttCoByProduct.disp_desc2 = true
            ttCoByProduct.pt_desc2   = pt_mstr.pt_dec2
         .
      end.

      for each cmt_det no-lock
         where cmt_det.cmt_domain = global_domain
         and  (cmt_indx = wo_cmtindx
         and   cmt_indx > 0:

         do i = 1 to 15:
         if cmt_cmmt[i] <> ""
         then do:
            /*put cmt_cmmt[i] at {&pos} skip.*/
         end.
      end.
   end. /*FOR EACH*/

END PROCUDURE. /* PROCEDURE PrintCoByProduct */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   define variable l_eff_date       as   date        no-undo.
   define variable l_wo_recno1      as   recid       no-undo.
   define variable l_leadtime       like pt_mfg_lead no-undo.
   define variable del-yn           like mfc_logical no-undo.
   define variable l_deliv          like wod_deliver no-undo.
   define variable l_wo_des         like pt_desc1    no-undo.
   define variable l_wo_um          like pt_um       no-undo.
   define variable l_wc_description like wc_desc     no-undo.
   define variable critical-part    like wod_part    no-undo.
   define variable l_wrnbr          like wo_nbr      no-undo.
   define variable l_wrlot          like wr_lot      no-undo.
   define variable l_wobatch        like wo_batch    no-undo.
   define variable l_l_msgdesc      like msg_desc    no-undo.
   define variable f_critical_flg   like mfc_logical no-undo.

   /* These variables will be used in .i files */
   /* Their names should not be changed */
   define variable l_ptstatus     like pt_status   no-undo.

   /* l = local */
   define variable l_wrnbr        like wo_nbr      no-undo.
   define variable l_wrlot        like wr_lot      no-undo.
   define variable l_des          like pt_desc1    no-undo.
   define variable l_base_id      like wo_base_id  no-undo.
   define variable l_qty          like wo_qty_ord  no-undo.
   define variable l_wo_qty       like wo_qty_ord  no-undo.
   define variable l_prev_status  like wo_status   no-undo.
   define variable l_prev_release like wo_rel_date no-undo.
   define variable l_prev_due     like wo_due_date no-undo.
   define variable l_prev_qty     like wo_qty_ord  no-undo.
   define variable l_comp         like ps_comp     no-undo.
   define variable l_wo_recno     as   recid       no-undo.
   define variable l_prd_recno    as   recid       no-undo.

   /* f = flag */
   define variable f_print_pick     like mfc_logical no-undo.
   define variable f_print_rte      like mfc_logical no-undo.
   define variable f_print_jp       like mfc_logical no-undo.
   define variable f_barcode        like mfc_logical no-undo.
   define variable f_move           like woc_move    no-undo.
   define variable f_upd_rel_date   like mfc_logical no-undo.

   assign
      l_wrnbr = GetFilterValue("ttReportOptions", "wrnbr", "")
      l_wrlot = GetFilterValue("ttReportOptions", "wrlot", "")
   .

   for first wo_mstr no-lock
      where wo_mstr.wo_domain = global_domain
        and wp_mstr.wo_lot    = l_wrlot:
   end.

   if not available wo_mstr then do:
      /* WORK ORDER NOT FOUND */
      {us/bbi/pxmsg.i &MSGNUM=503 &ERRORLEVEL=3}
      return.
   end.
   else do:
   if wo_mstr.wo_nbr <> l_wrnbr and l_wrnbr <> "" then do:
      /* LOT NUMBER BELONGS TO A DIFFERENT WORK ORDER */
      {us/bbi/pxmsg.i &MSGNUM=508 &ERRORLEVEL=3}
      return.
   end.

   /* CHECK IF ADD-WO TRANSACTION IS PERMITTED FOR ITEM BEFORE */
   /* CHANGING THE STATUS OF WORK ORDER FROM PLANNED TO ANY    */
   /* OTHER STATUS                                             */
   if wo_status = "p"
   then do:

      {us/px/pxrun.i &PROC = 'validateRestrictedStatus'
               &PROGRAM = 'wocmnrtn.p'
               &HANDLE = ph_wocmnrtn
               &PARAM = "(input wo_part,
                          ""ADD-WO"",
                          output l_ptstatus)"
               &NOAPPERROR = true
               &CATCHERROR = true}

      if return-value = {&APP-ERROR-RESULT}
      then do:
         /* RESTRICTED PROCEDURE FOR ITEM STATUS CODE */
         {us/bbi/pxmsg.i &MSGNUM = 358
                  &ERRORLEVEL = return-value
                  &MSGARG1 = l_ptstatus}

         next.
      end. /* IF return-value = {&APP-ERROR-RESULT} */

   end. /* IF wo_status = "P" */

   /* CHECK IF REL-WO TRANSACTION IS PERMITTED FOR ITEM BEFORE */
   /* CHANGING THE STATUS OF WORK ORDER FROM PLANNED TO ANY    */
   /* OTHER STATUS                                             */

   {us/px/pxrun.i &PROC = 'validateRestrictedStatus'
            &PROGRAM = 'wocmnrtn.p'
            &HANDLE = ph_wocmnrtn
            &PARAM = "(input wo_part,
                       ""REL-WO"",
                       output l_ptstatus)"
            &NOAPPERROR = true
            &CATCHERROR = true}

   if return-value = {&APP-ERROR-RESULT}
   then do:
      /* RESTRICTED PROCEDURE FOR ITEM STATUS CODE */
      {us/bbi/pxmsg.i &MSGNUM = 358
               &ERRORLEVEL = return-value
               &MSGARG1 = l_ptstatus}
      next.
   end. /* IF return-value = {&APP-ERROR-RESULT} */

   /* PREVENT ACCESS TO PROJECT ACTIVITY RECORDING WORK ORDERS */
   if wo_fsm_type = "PRM" then do:
      /* Controlled by PRM Module */
      {us/bbi/pxmsg.i &MSGNUM=3426 &ERRORLEVEL=3}
      next.
   end.

   /* PREVENT ACCESS TO CALL ACTIVITY RECORDING WORK ORDERS */
   if wo_fsm_type = "FSM-RO" then do:
      /* Controlled by Service/Support Module */
      {us/bbi/pxmsg.i &MSGNUM=7492 &ERRORLEVEL=3}
      next.
   end.

   assign
      l_wrnbr = wo_nbr
      l_wrlot = wo_lot
      l_des   = ""
   .

   find pt_mstr no-lock no-error no-wait
      where pt_mstr.pt_domain = global_domain
        and pt_part = wo_part.

   if available pt_mstr then l_des = pt_desc1.

   if index("PFBEAR",wo_status) = 0 then do:
      /* CAN ONLY RELEASE FIRM PLANNED OR ALLOCATED WORK ORDERS */
      {us/bbi/pxmsg.i &MSGNUM=516 &ERRORLEVEL=3}
      return.
   end.

   if wo_type = "C" and wo_nbr = "" then do:
      /* Work Order Type is Cumulative */
      {us/bbi/pxmsg.i &MSGNUM=5123 &ERRORLEVEL=3}
      return.
   end.

   /* Word Order type is flow */
   if wo_type = "w" then do:
      {us/bbi/pxmsg.i &MSGNUM=5285 &ERRORLEVEL=3}
      return.
   end.

   /* GET BASE PROCESS WO IF THIS IS A JOINT PRODUCT */
   if index("1234",wo_joint_type) > 0 then do:

      l_base_id = wo_base_id.

      find wo_mstr no-lock no-error
      where wo_mstr.wo_domain = global_domain
        and wo_lot            = l_base_id.

      if not available wo_mstr then do:
         /* JOINT PRODUCT NOT PRODUCED BY BOM/FORMULA */
         {us/bbi/pxmsg.i &MSGNUM=6546 &ERRORLEVEL=3}
         return.
      end.

   end.

   {us/bbi/gprun.i ""gpsiver.p""
      "(input wo_site, input ?, output return_int)"}
   if return_int = 0 then do:
      /* User does not have access to site */
      {us/bbi/pxmsg.i &MSGNUM=2710 &ERRORLEVEL=3 &MSGARG1=wo_site}
      return.
   end.

   if wo_qty_ord >= 0 then
      l_qty = max (wo_qty_ord - wo_qty_comp - wo_qty_rjct, 0).
   else
      l_qty = min (wo_qty_ord - wo_qty_comp - wo_qty_rjct, 0).

   assign
      l_wo_qty       = l_qty
      l_wo_recno     = recid(wo_mstr)
      l_comp         = wo_part
      l_prev_status  = wo_status
      l_prev_release = wo_rel_date
      l_prev_due     = wo_due_date
      l_prev_qty     = wo_qty_ord.

   /* SAVE prd_det RECID FOR BAR-CODES LATER */
   find prd_det where prd_dev = dev no-lock no-error.
   if available prd_det then
      l_prd_recno = recid(prd_det).

   /* Print Work Order Driver */
   printset:
   do transaction on error undo, leave:

      find first wo_mstr
         where recid(wo_mstr) = l_wo_recno
         exclusive-lock
      no-wait no-error.

      if locked wo_mstr
      then
         leave.

      if wo_rel_date <> today and
         wo_status <> "R"     and
         f_upd_rel_date = true
      then
         assign
            wo_shift = ""
            wo_seq = 0
            wo_rel_date = today.

      if index("PFBEA",wo_status) <> 0 then wo_status = "R".

      if wo_qty_ord >= 0 then
         l_qty = max (wo_qty_ord - wo_qty_comp - wo_qty_rjct, 0).
      else
         l_qty = min (wo_qty_ord - wo_qty_comp - wo_qty_rjct, 0).
      wo_qty = l_qty.
      assign
         l_prev_site = wo_site
         f_undo_all = no.

      if wo_joint_type <> "" and
         (index("PBFC",f_prev_status) > 0)  then do:
         /* CREATE/RE-ESTABLISH ITS EFFECTIVE JOINT WOS. */
         {us/bbi/gprun.i ""wowomtf.p""}
         find wo_mstr no-lock where recid(wo_mstr) = wo_recno.
         if f_undo_all then undo, retry.
      end.

      /* UPDATE OTHER CO/BY WO STATUS */
      else
      if wo_joint_type <> "" and (index("EA",prev_status) > 0)
         then do:
            assign
               f_joint_dates = no
               f_joint_qtys = no
               f_any_issued = no
               f_any_feedbk = no.
            {us/bbi/gprun.i ""wowomti.p""}
            find wo_mstr no-lock where recid(wo_mstr) = wo_recno.
            if undo_all then undo, retry.
         end.

      joint_type = wo_joint_type.
      {us/bbi/gprun.i ""wowomta.p""}

      assign
         picklistprinted = no
         routingprinted = no
         jpprinted = no.

      if f_undo_all = no then do:
         if print_jp and
            jp_1st_last_doc and wo_joint_type <> "" then do:
            /* PRINT JOINT PRODUCTS EXPECTED RECEIPTS */
               wo_recno = recid(wo_mstr).
               run PrintCoByProduct(input-output f_jpprinted,l_deliv,l_wo_des,l_wo_qty,l_wo_um,l_wo_recno).
         end.
         if print_pick then do:
               wo_recno = recid(wo_mstr).
            run PrintPickList(input-output f_picklistprinted,l_deliv,l_wo_des,l_wo_qty,l_wo_um,l_wo_recno).
         end.
         if print_rte then do:
               wo_recno = recid(wo_mstr).
            run PrintRouting(input-output f_routingprinted,l_deliv,f_barcode,l_wo_des,l_wo_qty,l_wo_um,l_wo_recno,l_print_loc,l_prd_recno).
         end.
         if print_jp and
            not jp_1st_last_doc and wo_joint_type <> "" then do:
            /* PRINT JOINT PRODUCTS EXPECTED RECEIPTS */
               wo_recno = recid(wo_mstr).
            run PrintCoByProduct(input-output f_jpprinted,l_deliv,l_wo_des,l_wo_qty,l_wo_um,l_wo_recno).
         end.
      end.

      if (f_print_pick and not f_picklistprinted)
      or (f_print_rte and not f_routingprinted)
       or f_undo_all = yes
      then do:
         page.
         if undo_all then do:
            /* KEY ITEM NOT AVAILABLE, WORK ORDER NOT RELEASED  */
            {us/bbi/pxmsg.i &MSGNUM=4984 &ERRORLEVEL=2 &MSGARG1=wo_nbr}
            {us/bbi/pxmsg.i &MSGNUM=989  &ERRORLEVEL=1 &MSGARG1=critical-part}
            if not batchrun then pause.
            wo_status = prev_status.
         end.
         if (print_pick and not picklistprinted) then
         do:
            /* ***NO PICKLIST WAS PRINTED FOR WORK ORDER */
            {us/bbi/pxmsg.i &MSGNUM=3773 &ERRORLEVEL=1 &MSGBUFFER=l_msgdesc
                     &MSGARG1=wo_nbr}
            put unformatted
               l_msgdesc skip.
         end. /* IF (PRINT_PICK AND NOT PICKLISTPRINTED) THEN */

         if (print_rte and not routingprinted) then
         do:
            /* ***NO ROUTING WAS PRINTED FOR WORK ORDER */
            {us/bbi/pxmsg.i &MSGNUM=3803 &ERRORLEVEL=1 &MSGBUFFER=l_msgdesc
                     &MSGARG1=wo_nbr}
            put unformatted
               l_msgdesc skip.
         end. /* IF (PRINT_RTE AND NOT ROUTINGPRINTED) THEN */

      end.

      if undo_all = no then do:

         wo_order_sheet_printed = yes.
         if wo_status <> "R" then wo_status = "R".

         if move then do:
            move = no.
            find first wr_route  where wr_route.wr_domain = global_domain and
            wr_lot = wo_lot
               and wr_nbr = wo_nbr no-error.
            if available wr_route and wr_status = ""
            then do:
               wrlot = wr_lot.
               {us/mf/mfopmv.i wr_qty_ord "no"}
            end.
            move = yes.
         end.

         for first pt_mstr
            where pt_domain = global_domain
            and   pt_part   = wo_part
         no-lock:
            for first clc_ctrl
            no-lock:
            end. /* FOR FIRST clc_ctrl */

            if not available clc_ctrl
            then do:
               {us/bbi/gprun.i ""gpclccrt.p""}
               for first clc_ctrl
               no-lock:
               end. /* FOR FIRST clc_ctrl */
            end. /* IF NOT AVAILABLE clc_ctrl  */

            /* ATTRIBUTES DATA */
            if available clc_ctrl
               and (lookup(wo_status,"P,B,C,") = 0)
            then do:
               if pt_auto_lot = yes
                  and pt_lot_ser = "L"
                  and pt_lot_grp = ""
               then do:
                  if wo_lot_next = ""
                  then
                     wo_lot_next =   wo_lot.
                  wo_lot_rcpt = no.
               end. /* IF pt_auto_lot    = yes */

               if (wo_type = "E"
                   or wo_type = "R")
               then
                  wo_lot_rcpt = no.

               if wo_type <> "w"
               then do:
                  if wo_lot_rcpt
                  then
                     wo_lot_next = "".
               end. /* IF wo_type <> "w" */
            end. /* IF AVAILABLE clc_ctrl */
         end. /* FOR FIRST pt_mstr */
      end. /* IF undo_all */

      if undo_all then do:
         undo printset, leave.
      end.
   end.

   phReportResults = dataset dsReportResults:handle.

END PROCEDURE.
